/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.sales;

import dao.BaseDaoHibernate;
import java.util.Date;
import model.sales.SalesMst;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jay
 */
@Repository
public class SalesDao extends BaseDaoHibernate<SalesMst>{
    
    private static final Logger log = Logger.getLogger(SalesDao.class);
    
    public boolean saveData(SalesMst domain) {
        Session session = sessionFactory.openSession();
        boolean saved = false;
        try {
            session.beginTransaction();
            domain.setTransTime(new Date());                    
            domain.setSalesDetails(domain.getSalesDetails());
            sessionFactory.getCurrentSession().saveOrUpdate(domain);
            
            long orderId = domain.getId();
            for (int i=0; i<domain.getSalesDetails().size(); i++) {
                domain.getSalesDetails().get(i).getId().setId(orderId);
                domain.getSalesDetails().get(i).setSeq(i);
            }
            
            for (int i=0; i<domain.getSalesPromos().size(); i++) {
                domain.getSalesPromos().get(i).getId().setId(orderId);
                domain.getSalesPromos().get(i).getId().setSeq(i);
            }
            
            domain.getSalesPayment().setId(orderId);

            session.getTransaction().commit();
            saved=true;
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); 
            log.error("gagal", e);
            java.util.logging.Logger.getLogger(SalesDao.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        } finally {
            if (session != null) { session.close();}
            return saved;
        }
    }
    
}
